import Config from "./config";
export default class HTTP {
  request(params) {
    params.method = params.method || 'GET';
    return new Promise((resolve, reject) => {
      wx.request({
        url: Config.base_url + params.url,
        method: params.method,
        data: params.data,
        success: res => {
          // console.log('success->', res);
          if (params.data.TableName === 'jyJinnang') {
            if (typeof (res.data) === 'string') {
              res.data = res.data.replace(/\\/g, '');
              try {
                res.data = JSON.parse(res.data);
              } catch (error) {
                console.error(error)
              }
            }
          }
          const { data: { head, body } } = res;
          if (head.IsSuccess && head.IsSuccess === '1') {
            resolve(body)
          } else {
            reject(head.Msg);
            wx.showToast({
              title: head.Msg,
              icon: 'none',
              duration: 2000,
            })
          }
        },
        fail: err => {
          console.error(err)
          reject(err);
          wx.showToast({
            title: err,
            icon: 'none',
            duration: 2000,
          })
        }
      })
    })
  }
}
